'''
Created on Nov 19, 2010

@author: namikata
'''
from pyglet import window
from pyglet.window import key
from pyglet import clock
from pyglet import font
from game.base.Keyboard import Keyboard
from game.base.Mouse import Mouse, LEFT_BUTTON, MIDDLE_BUTTON, RIGHT_BUTTON
from game.base.GameEntity import *
from game.screen.World import World
from game.screen.Cursor import Cursor
from gameobjects.vector2 import Vector2
from game.screen.Player import Player
from game.screen.UI_Player import UI_Player
from game.effect.EffectList import Effect_Trail

main = None

class Main(window.Window):
    
    fps = 0
    log_font = None
    log_text = None
    fullscreen = False
    debugging = False
    keyboard = None
    mouse = None
    world = None
    background_color = (0,0.5,0,1)
    
    def __init__(self, *args, **kwargs):
        
        #Let all of the standard stuff pass through
        window.Window.__init__(self, *args, **kwargs)
        
        self.keyboard = Keyboard()
        self.mouse = Mouse()
        self.log_font = font.load('Arial', 12)
        self.fps = 0
        self.world = World()
        self.world.bounds = (Vector2(0,0),Vector2(self.width, self.height))
        self.fullscreen = False
        self.debugging = False

    def on_mouse_motion(self,x, y, dx, dy):
        self.mouse.update_location(Vector2(dx,dy))
        
    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
        self.mouse.update_location(Vector2(dx, dy))
        self.mouse.update_buttons(buttons)
        
    def on_mouse_press(self, x, y, button, modifiers):
        self.mouse.update_button_state(button, True)
                
    def on_mouse_release(self, x, y, button, modifiers):
        self.mouse.update_button_state(button, False)
                
    def on_mouse_scroll(self, x, y, scroll_x, scroll_y):
        self.mouse.update_wheel(scroll_y)
                
    def run(self, resFile = "res.txt"):
        #configure world
        pyglet.gl.glClearColor(*self.background_color)
        
        #capture mouse
        self.set_exclusive_mouse()
        
        self.world.load_resources(resFile)
        self.world.fillimage = self.world.resources.back
                               
        player = Player(self.world,"player1",self.world.resources.dic["ship"],KCLASS_PLAYER,KSCLASS_PLAYERHUMAN,(400,400))
        player.load_armory("weapons.xml")
        cursor = Cursor(self.world,"cursor",self.world.resources.cursor,KCLASS_CURSOR,KSCLASS_CURSORPLAYER,Vector2(0,0))
        player.cursor = cursor
        player_ui = UI_Player(KCLASS_UI,KSCLASS_UIPLAYER,player,self.world.resources.dic["none"], True)
        trail_effect = Effect_Trail(10,0.1)
        player.effect_library.add_effect(trail_effect)
        self.world.add_entity(player)
        self.world.add_entity(cursor)
        self.world.add_entity(player_ui)

        #add keyboard status update to event handler
        keys = key.KeyStateHandler()
        self.push_handlers(keys)
                                
        while not self.has_exit:
            self.dispatch_events()
            self.clear()
            self.world.fillimage.blit(self.width//2,self.height//2)
            
            self.keyboard.update(keys)
            self.mouse.update()
                    
            if self.keyboard.get_key(key.ESCAPE) == 1:
                self.has_exit = True
            if self.keyboard.get_key(key.F) == 1:
                self.fullscreen = not self.fullscreen
                self.set_fullscreen(self.fullscreen)
            if self.keyboard.get_key(key.O) == 1:
                self.debugging = not self.debugging
            if self.keyboard.get_key(key.P) == 1:
                self.world.paused = not self.world.paused
            if self.keyboard.get_key(key.U) == 1:
                self.world.toggle_ui()
            if self.keyboard.get_key(key.NUM_ADD) == 1:
                self.world.time_scale = min(5,self.world.time_scale+0.1)
            if self.keyboard.get_key(key.NUM_SUBTRACT) == 1:
                self.world.time_scale = max(0.1,self.world.time_scale-0.1)
                    
            seconds_elapsed = clock.tick()
            transformed_time = seconds_elapsed * self.world.time_scale
                            
            self.fps = clock.get_fps()
            self.debug()

            self.world.update(transformed_time, self.keyboard, self.mouse)
            self.world.render()                          
                   
            self.flip()
                
    def debug(self):
        step = 0
        step = self.write("fps:%d" % (self.fps),step)
        
        if not self.debugging:
            return
        
        player = self.world.get_by_name("player1")
        cursor = self.world.get_by_name("cursor")
        #player_ui = self.world.get_by_name("player1_ui")
        step = self.write("mouse.location:%r" % (self.mouse.location), step)
        step = self.write("mouse.pressed:(%r,%r,%r)" % (self.mouse.get_pressed(LEFT_BUTTON),self.mouse.get_pressed(MIDDLE_BUTTON),self.mouse.get_pressed(RIGHT_BUTTON)), step)
        step = self.write("mouse.scroll:%r" % (self.mouse.get_scroll()), step)
        if player:
            step = self.write("player.position:%r" % (Vector2(*player.sprite.position)),step)
            step = self.write("player.speed:%d" % (player.speed),step)
            step = self.write("player.direction:%s" % (player.direction),step)
            step = self.write("player.rotation:%r" % (player.sprite.rotation),step)
            step = self.write("player.scale:%r" % (player.sprite.scale),step)
            step = self.write("armory.cooldown:%r" % (player.armory.cooldown),step)
            step = self.write("armory.energy:%r" % (player.armory.energy),step)
            step = self.write("armory.weapon1:%r" % (player.armory.current_weapon_1.name),step)
            step = self.write("armory.weapon2:%r" % (player.armory.current_weapon_2.name if player.armory.current_weapon_2 else u"None"),step)
            step = self.write("armory.shooting:%r" % (player.armory.shooting),step)
            step = self.write("armory.charging:%r" % (player.armory.charging),step)
            step = self.write("armory.charge:%r" % (player.armory.charge),step)
        if cursor:
            step = self.write("cursor.location:%s" % (Vector2(cursor.sprite.position)),step)
        step = self.write("paused:%r" % (self.world.paused),step)
        step = self.write("entities:%r" % (len(self.world.entities)),step)
        #if player_ui:
        #    step = self.write(self.screen, self.logfont, "player_ui.armor:%r" % (player_ui.armor),True,(255,255,255),(0,0,0),step)
        #    step = self.write(self.screen, self.logfont, "player_ui.armor_max:%r" % (player_ui.armor_max),True,(255,255,255),(0,0,0),step)
        #    step = self.write(self.screen, self.logfont, "player_ui.energy:%r" % (player_ui.energy),True,(255,255,255),(0,0,0),step)
        #    step = self.write(self.screen, self.logfont, "player_ui.energy_max:%r" % (player_ui.energy_max),True,(255,255,255),(0,0,0),step)
        #    step = self.write(self.screen, self.logfont, "player_ui.cooldown:%r" % (player_ui.cooldown),True,(255,255,255),(0,0,0),step)
        #    step = self.write(self.screen, self.logfont, "player_ui.cooldown_max:%r" % (player_ui.cooldown_max),True,(255,255,255),(0,0,0),step)
        step = self.write("world.energy_pool:%r" % (self.world.energy_pool),step)
        step = self.write("world.time_scale:%r" % (self.world.time_scale),step)
        
        if player and player.effect_library:
            print len(player.effect_library.eff)
        #    step = self.write(self.screen, self.logfont, "player.effects:",True,(255,255,255),(0,0,0),step)
        #    step = self.write(self.screen, self.logfont, "library_id:%r" % (player.effect_library.random_id),True,(255,255,255),(0,0,0),step)
        #    for effect in player.effect_library.eff.itervalues():
        #        step = self.write(self.screen, self.logfont, "effect[%d].duration:%r" % (effect.effect_class,effect.duration),True,(255,255,255),(0,0,0),step)
        #        step = self.write(self.screen, self.logfont, "effect[%d].time_elapsed:%r" % (effect.effect_class,effect.time_elapsed),True,(255,255,255),(0,0,0),step)
        if cursor and cursor.effect_library:
            print len(cursor.effect_library.eff)
        #    step = self.write(self.screen, self.logfont, "library_id:%r" % (cursor.effect_library.random_id),True,(255,255,255),(0,0,0),step)
        #    step = self.write(self.screen, self.logfont, "cursor.effects:",True,(255,255,255),(0,0,0),step)
        #    for effect in cursor.effect_library.eff.itervalues():
        #        step = self.write(self.screen, self.logfont, "effect[%d].duration:%r" % (effect.effect_class,effect.duration),True,(255,255,255),(0,0,0),step)
        #        step = self.write(self.screen, self.logfont, "effect[%d].time_elapsed:%r" % (effect.effect_class,effect.time_elapsed),True,(255,255,255),(0,0,0),step)
        
    def write(self, text, step):
        if not self.log_text:
            self.log_text = pyglet.text.Label(font_name="Verdana", font_size=12)
        self.log_text.y = 15*step
        self.log_text.text = text
        self.log_text.draw()
        return step + 1
    
if __name__ == '__main__':
    main = Main(width=1024, height=768)
    main.run()
    
    